package com.kyzh.core.pager.weal.gift

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.Game
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemGiftBinding
import com.gushenge.core.base.fragment.BaseFragment
import kotlinx.android.synthetic.main.fragment_giftitem.*
import org.jetbrains.anko.support.v4.startActivity

class GiftItemFragment : BaseFragment() {
    private var param1: Int = 0

    private val beans = arrayListOf<Game>()
    private val adapter = Adapter(R.layout.item_gift,beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getInt("position")
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_giftitem, container, false)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int=0) =
                GiftItemFragment().apply {
                    arguments = Bundle().apply {
                        putInt("position", param1)
                    }
                }
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rvList.layoutManager = GridLayoutManager(requireContext(),1)
        rvList.adapter = adapter
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=false
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<Game>): BaseQuickAdapter<Game, BaseDataBindingHolder<ItemGiftBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemGiftBinding>, item: Game) {
            holder.dataBinding?.let {
                it.data  =item

            }
            holder.itemView.setOnClickListener {
                startActivity<GiftItemActivity>("gid" to item.gameId,"position" to param1.toString())
            }
        }

    }



    fun getData(){
        UserRequest.giftCenter(MMKVConsts.giftSearch,type = param1,p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        UserRequest.giftCenter(MMKVConsts.giftSearch,type = param1,1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
}